#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<string> maze(n, string(m, '*'));
    vector<vector<bool>> use(n, vector<bool>(m, false));
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 -= 1, y1 -= 1, x2 -= 1, y2 -= 1;
    for (int i = 0, a; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> maze[i][j];
        }
    }
    queue<std::pair<int, int>> q;
    q.emplace(x1, y1);
    use[x1][y1] = true;
    int step = 0;
    while (!q.empty())
    {
        int size = q.size();
        for (int i = 0; i < size; i++)
        {
            auto &[_x, _y] = q.front();
            q.pop();
            if (_x == x2 && _y == y2)
            {
                cout << step << endl;
                return 0;
            }
            for (int k = 0; k < 4; k++)
            {
                int x = _x + dx[k], y = _y + dy[k];
                if (x < 0 || x >= n || y < 0 || y >= m || maze[x][y] == '*' || use[x][y] == true)
                {
                    // if(x == 1 && y == 4)
                    // {
                    //     cout << endl;
                    //     cout << "---------------" << endl;
                    //     cout << n << m << maze[x][y] << " " << use[x][y] << endl;
                    //     cout << "---------------" << endl;
                    // }
                    // cout << "false" << endl;
                    continue;
                }
                // cout << "ture" << endl;
                q.emplace(x, y);
                use[x][y] = true;
            }
        }
        step += 1;
    }
    cout << -1 << endl;
    return 0;
}